const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const webpack = require('webpack');
const extractSass = new ExtractTextPlugin({
    filename: "[name].css"
});
var SpritesmithPlugin = require('webpack-spritesmith');
module.exports = {
    entry: {
        app : './dev/app.js'
        // share : './dev/js/share.js'
    },
    devtool : 'inline-source-map',
    devServer : {
        contentBase : './dev',
        hot : true,
        hotOnly: true,
        host: "192.168.0.237",
        disableHostCheck: true
        // proxy: {
        //     '/redirect/*': {
        //         target: 'http://mobi.4399tech.com',
        //         changeOrigin: true,
        //         secure: false,
        //     }
        // }
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env']
                    }
                }
            },
            {
                test: /\.scss$/,
                use: extractSass.extract({
                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "sass-loader"
                    }],
                    // use style-loader in development
                    fallback: "style-loader"
                })
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 333333333333
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        extractSass,
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
            filename : 'index.html',
            title:"扭蛋活动",
            chunks:["app"],
            template: './dev/index.html'
        }),
        // new HtmlWebpackPlugin({
        //     filename : 'share.html',
        //     title:"扭蛋活动",
        //     chunks:["share"],
        //     template: './dev/share.html'
        // }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new SpritesmithPlugin({
            // 目标小图标
            src: {
                cwd: path.resolve(__dirname, './dev/img/icon'),
                glob: '*.png'
            },
            // 输出雪碧图文件及样式文件
            target: {
                image: path.resolve(__dirname, './release/sprites/sprite.png'),
                css: path.resolve(__dirname, './release/sprites/sprite.css')
            },
            // 样式文件中调用雪碧图地址写法
            apiOptions: {
                cssImageRef: '../sprites/sprite.png'
            },
            spritesmithOptions: {
                algorithm: 'top-down'
            }
        })
    ],
    output: {
        filename: '[name].js',
        path : path.resolve(__dirname,'release')
    }
}